
package com.v1.video.util;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.v1.video.domain.CityChoiceinfo;

/**
 * 查询电话号码归属地
 * 
 * @author Administrator
 */
public class CityChoiceDBUtil {
//    public String path = "/data/data/com.example.citychoose/files/city.db";
    private String path ;
    
    public CityChoiceDBUtil(Context context){
        this.path = context.getFilesDir().toString()+"/city.db";
    }

    /**
     * 对香港和澳门特殊处理，只保留省份名称
     */
    public List<CityChoiceinfo> getCitys() {
    	
        SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);

        String sql = " select p.proname,z.zonename as cityname,p.charindex      " +
        		"     from T_Province as p, T_city as c, T_zone as z       " +
        		"     where p.proremark='直辖市' and p.[ProSort]=c.proid and z.cityid=c.citysort" +
        		"      UNION ALL " +
        		"      select p.proname,c.CityName as cityname,p.charindex" +
        		"      from T_Province as p,T_City as c " +
        		"     where c.ProID=p.prosort and p.[ProRemark]<>'直辖市'" +
        		"      order by p.[charindex]";
     
        List<CityChoiceinfo> lists = new ArrayList<CityChoiceinfo>();
        if (db.isOpen()) {
            Cursor cursor = db.rawQuery(sql, null);
            while (cursor.moveToNext()) {
                CityChoiceinfo g = new CityChoiceinfo();
                String pro = cursor.getString(0);
                String city = cursor.getString(1);
                g.setProname(pro);
                if(pro.equals(city)){
                    g.setCityname("");
                }else{
                    g.setCityname(city);
                }
                g.setCharindex(cursor.getString(2));
                lists.add(g);
            }
            cursor.close();
            db.close();
        }

        return lists;

    }
    

    
}


